package com.example.likestarlibrary;

import com.example.likestarlibrary.util.NumCalcUtil;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.render.Path;
import ohos.agp.utils.Point;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * 飘心路径
 */
public abstract class TCAbstractPathAnimator {
    protected final Config mConfig;
    final double val = Math.random();
    final int sval1 = (int)(val*7);
    public TCAbstractPathAnimator(Config config) {
        mConfig = config;
    }

    public float randomRotation() {
        final double val = Math.random();
        final int srand = (int)(val*7);
        return NumCalcUtil.subtract(srand * 28.6F,14.3F);
    }

    public Path createPath(AtomicInteger counter, Component view, int factor) {
        int y = view.getHeight() - mConfig.initY;
        int y2 = counter.intValue() * 15 + mConfig.animLength * factor + sval1;
        factor = y2 / mConfig.bezierFactor;
        int y3 = y - y2;
        y2 = y - y2 / 2;
        Path p = new Path();
        p.moveTo(mConfig.initX, y);
        p.cubicTo(new Point(mConfig.initX, y - factor), new Point(0, y2 + factor), new Point(0, y2));
        p.moveTo(0, y2);
        p.cubicTo(new Point(0, y2 - factor), new Point(0, y3 + factor), new Point(0, y3));
        return p;
    }

    public abstract void start(Component child, ComponentContainer parent);

    public static class Config {
        public int initX;
        public int initY;
        public int animLengthRand;
        public int bezierFactor;
        public int xPointFactor;
        public int animLength;
        public int heartWidth;
        public int heartHeight;
        public int animDuration;

        static public Config fromTypeArray(AttrSet typedArray, float x, float y, int pointx, int heartWidth, int heartHeight) {
            Config config = new Config();
            config.initX = AttrUtils.getDimensionFromAttr(typedArray, "heart_anim_init_x", (int) x);
            config.initY = AttrUtils.getDimensionFromAttr(typedArray, "heart_anim_init_y", (int) y);
            config.animLength = AttrUtils.getDimensionFromAttr(typedArray, "heart_anim_length", 0);
            config.animLengthRand = AttrUtils.getDimensionFromAttr(typedArray, "heart_anim_length_rand", 0);
            config.bezierFactor = AttrUtils.getIntegerFromAttr(typedArray, "heart_anim_bezier_factor", 0);
            config.xPointFactor = pointx;
            config.heartWidth = heartWidth;
            config.heartHeight = heartHeight;
            config.animDuration = AttrUtils.getIntegerFromAttr(typedArray, "anim_duration", 0);
            return config;
        }

    }

}

